package com.sunricher.telinkblemesh.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.sunricher.telinkblemesh.R;
import com.sunricher.telinkblemeshlib.NfcToolManager;
import com.sunricher.telinkblemeshlib.callback.NfcToolCallback;

public class ResetNfcActivity extends Activity {

    private NfcAdapter adapter;
    private PendingIntent pendingIntent;

    private NfcToolCallback callback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_nfc);

        adapter = NfcAdapter.getDefaultAdapter(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            pendingIntent = PendingIntent.getActivity(
                    this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_MUTABLE);
        } else {
            pendingIntent = PendingIntent.getActivity(
                    this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        }
        callback = new NfcToolCallback() {
            @Override
            public void nfcToolManagerDidFailed(NfcToolManager manager) {
                System.out.println("did failed");
                Toast.makeText(ResetNfcActivity.this, "did failed", Toast.LENGTH_SHORT).show();
                new Handler(Looper.getMainLooper()).postDelayed(() -> finish(), 1000L);
            }

            @Override
            public void nfcToolManagerGotUnsupportedDevice(NfcToolManager manager) {
                System.out.println("got unsupported device");
                Toast.makeText(ResetNfcActivity.this, "got unsupported device", Toast.LENGTH_SHORT).show();
                new Handler(Looper.getMainLooper()).postDelayed(() -> finish(), 1000L);
            }

            @Override
            public void nfcToolManagerDidSucceeded(NfcToolManager manager) {
                System.out.println("did succeeded");
                Toast.makeText(ResetNfcActivity.this, "did succeeded", Toast.LENGTH_SHORT).show();
                new Handler(Looper.getMainLooper()).postDelayed(() -> finish(), 1000L);
            }
        };
        NfcToolManager.getInstance().setCallback(callback);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (adapter == null) {
            return;
        }
        try {
            adapter.enableForegroundDispatch(this, pendingIntent, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void onPause() {
        super.onPause();

        if (adapter == null) {
            return;
        }
        adapter.disableForegroundDispatch(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        callback = null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (tag == null) {
            System.out.println("Tag is null, connect failed.");
            return;
        }

        NfcToolManager.getInstance().resetDevice(tag);
    }
}